// JavaScript Document
$(function(){  
  	function judge(text){
		var sl=$('.sl').attr('value');
		if(!sl){
			        $('.modal-title').html(text);
                    $('#myModal').modal('toggle');
					return false;
			}
		else{
			return true;
			}		  
		}
	var setting={
		
			url:lookUrl,
			dataType:'json',
			type:'POST',
			data:{'aTime':$('#contents1').attr('times'),'aId':$('#contents1').attr('value')},
			success:function(data){
					subat(data['uimg'],data['uname'],data['ucontent'],data['utime'],data['ftime'],data['Aid'],'');
					$.ajax(setting);
				},
		}	
//$.ajax(setting);
	function subat( uimg,uname,ucontent,utime,ftime,Aid,Uid) {
	html1 = '<div class="discuss">';
	html1 += '<div class="disc-user">';
	html1 += '<a href="mypageUser/hispage/'+Uid+'.html"><img src="' + uimg + '" width="45" title="'+uname+'" class="img-circle"/></a><br />';
	html1 += '    </div>';
	html1 += '    <div class="disc-body">';
	html1 += '        <div class="disc-uname">';
	html1 += uname+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	html1 += '<span style="font-size:10px; color:#ccc;" class="discussTime" value="'+ftime+'">'+utime+'</span>';
	html1 += '        </div>';
	html1 += '        <div class="disc-cont">';
	html1 += ucontent;
	html1 += '        </div>';
	html1 += '    </div>';
	html1 += '    <div class="clearfix"></div>';
	html1 += '</div>';
	$('.discuss-list').append(html1);
	$('#woqu').slideUp();
	$('.fake-textarea').fadeIn();
}
  var ctrlPressed=false;
  
  
  
  //socket连接
  var discuss = new io.connect('http://122.10.87.69:889');
  discuss.on('needlogin',function(){
		discuss.emit('login',md5("discuss" + md5($('#contents1').attr('value'))));
  });
  discuss.on('discuss mess',function(data){
	  
		if($('#editor').attr('sending') == 'true')
			$('#editor').attr('sending','false');
		else
		subat( data['uimg'],data['uname'],data['ucontent'],data['utime'],data['ftime'],'',data['uid']);
  });
  
	$(document).keydown(function(event){
		if(event.which==17)ctrlPressed=true;
		if(ctrlPressed){
			if(event.which==13){
				$('.discussImmedia').click();
			}
		}
	});
		$(document).keyup(function(event){
			if(event.which==17)ctrlPressed=false;
		});

//提交并通知nodejs
$(document).on("click",'.discussImmedia',function(){
	if($('#editor').attr("idt") == "true")
		return;$("#editor").focus();
	var content=$('#editor').html();
	if(content.length>0){
	var articleId=$('#contents1').attr('value');
	var aTime=$('#contents1').attr('times');
	$.ajax({
		url:rewordUrl,
		type:'POST',
		dataType:"json",
		data:{'content':content,'articleId':articleId,'aTime':aTime},
		success:function(data){
			$('#editor').empty();
			var ndata = {};
			ndata['msg'] = data;
			ndata['to'] = md5("discuss" + md5($('#contents1').attr('value')));
			discuss.emit('discuss',ndata);
			$('#editor').attr('sending','true');
			subat( data['uimg'],data['uname'],data['ucontent'],data['utime'],data['ftime'],'',data['uid']);
		},
		error:function(e){
			alert('发送失败！刷新一个试试？');
		}
	});
	}
});

$('.disorder').click(function(){
	var text='欲知后事如何，请先拿着板凳排排坐';
    if(!judge(text))return false;
	var father=$('#contents1').attr("value");
	var title=$('.title').html();
	var content=$('.contents2').html();
	$.ajax({
		url:urlOrder,
		type:'POST',
		data:{'articleId':father,'title':title,'content':content},
		success: function(data){
			$('.disorder').html("已订阅");
			$('.disorder').removeClass('disorder');
			}
		});
	});
$('.join').click(function(){//点击申请加入
var text='看来你有话说，我们等你一起讨论';
    if(!judge(text))return false;
	var article=$('#contents1').attr("value");
	$.ajax({
		url:urlJoin,
		type:'POST',
		data:{'article':article},
		success: function(data){
			if(data){
		$('.join').html("√ 已申请");	
			}
		}
		});
	});
	
});